<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- saved from url=(0028)http://jdbi.org/archive.html -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>


<title>abacus-common</title>

<script type="text/javascript" src="./css/ga.js"></script>
<script type="text/javascript">
	if (top.location != self.location)
		top.location.replace(self.location);
</script>

<link href="./css/css" rel="stylesheet" type="text/css"></link>
<link href="./css/css(1)" rel="stylesheet" type="text/css"></link>
<link rel="stylesheet" href="./css/syntax.css" type="text/css"></link>
<link rel="stylesheet" href="./css/screen.css" type="text/css"></link>

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-65247258-1', 'auto');
  ga('send', 'pageview');
</script>

</head>
<body>
	<div id="site">
		<h1>
			<a href="./index.html">abacus-common</a> <span id="navigation"> <a href="./docs.html">Docs</a> | <a href="./download.html">Download</a>
				| <a href="./api-docs/index.html">Javadoc</a> | <a href="./faq.html">FAQ</a> | <a href="./community.html">Community</a>
			</span>
		</h1>
	</div>

	<div id="content">

		<div id="page">
			<h1>Code Generation</h1>
			<p>
				It's easy to generate the table/entity xml mapping, pretty-formatted entity classes, and classes for property(column) names and sql mapper ids by: <a
					href="./api-docs/com/landawn/abacus/util/CodeGenerator.html">CodeGenerator</a> in abacus-common. All can be done in several minutes by below steps:
			</p>


			<li><b>Step 1: Prepare database/tables by the scripts for: </b><a href="./MySQL.sql">MySQL</a>, <a href="./PostgreSQL.sql">PostgreSQL</a>, <a
				href="./H2.sql">H2</a>, <a href="./HSQLDB.sql">HSQLDB</a>, <a href="./Oracle.sql">Oracle</a>, <a href="./DB2.sql">DB2</a>, <a
				href="./SQLServer.sql">SQLServer</a>
			</li>
			
			
			<p></p>
			<li><b>Step 2: Generate the xml file:</b></li>
<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">static</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">database2EntityDefinitionXml</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
    String dbName <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;codes&quot;</span><span style="color: #666666">;</span>
    String packageName <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;com.landawn.abacus.entity&quot;</span><span style="color: #666666">;</span>
    File entityXmlFile <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> File<span style="color: #666666">(</span><span style="color: #BA2121">&quot;./resources/codesEntityDefinition.xml&quot;</span><span style="color: #666666">);</span>

    Connection conn <span style="color: #666666">=</span> JdbcUtil<span style="color: #666666">.</span><span style="color: #7D9029">createConnection</span><span style="color: #666666">(</span>url<span style="color: #666666">,</span> user<span style="color: #666666">,</span> password<span style="color: #666666">);</span>
    Map<span style="color: #666666">&lt;</span>String<span style="color: #666666">,</span> String<span style="color: #666666">&gt;</span> attrs <span style="color: #666666">=</span> N<span style="color: #666666">.</span><span style="color: #7D9029">newHashMap</span><span style="color: #666666">();</span>
    attrs<span style="color: #666666">.</span><span style="color: #7D9029">put</span><span style="color: #666666">(</span>EntityDefEle<span style="color: #666666">.</span><span style="color: #7D9029">PACKAGE</span><span style="color: #666666">,</span> packageName<span style="color: #666666">);</span>

    <span style="color: #008000; font-weight: bold">try</span> <span style="color: #666666">{</span>
        SQLDatabase database <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> SQLDatabase<span style="color: #666666">(</span>conn<span style="color: #666666">,</span> dbName<span style="color: #666666">);</span>

        CodeGenerator<span style="color: #666666">.</span><span style="color: #7D9029">database2EntityDefinitionXml</span><span style="color: #666666">(</span>database<span style="color: #666666">,</span> entityXmlFile<span style="color: #666666">,</span> attrs<span style="color: #666666">);</span>
    <span style="color: #666666">}</span> <span style="color: #008000; font-weight: bold">finally</span> <span style="color: #666666">{</span>
        JdbcUtil<span style="color: #666666">.</span><span style="color: #7D9029">closeQuietly</span><span style="color: #666666">(</span>conn<span style="color: #666666">);</span>
    <span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>


			<p></p>
			<li><b>Step 3: Modify the generated xml file accordingly per the requirements. </b>Here are the sample of generated xml file: 
			<a href="./codesEntityDefinition.xml">codesEntityDefinition.xml</a> and schema of entity definition: <a href="./EntityDefinition.xsd">EntityDefinition.xsd</a>. You can also manually prepare the xml file without step 1 and 2 if the codes is not generated for database/tables.</li>


			<p></p>
			<li><b>Step 4: Generate the entity classes by the xml file:</b></li>
<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">static</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">entityDefinitionXml2Class</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
    String srcPath <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;./src/&quot;</span><span style="color: #666666">;</span>
    String domainName <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;codes&quot;</span><span style="color: #666666">;</span>
    File entityXmlFile <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> File<span style="color: #666666">(</span><span style="color: #BA2121">&quot;./resources/codesEntityDefinition.xml&quot;</span><span style="color: #666666">);</span>
    EntityMode entityMode <span style="color: #666666">=</span> EntityMode<span style="color: #666666">.</span><span style="color: #7D9029">POJO_WITH_PROP_NAME_TABLE</span><span style="color: #666666">;</span>
    Method propName2MethodName <span style="color: #666666">=</span> N<span style="color: #666666">.</span><span style="color: #7D9029">getDeclaredMethod</span><span style="color: #666666">(</span>CodeGeneratorTest<span style="color: #666666">.</span><span style="color: #7D9029">class</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;propName2MethodName&quot;</span><span style="color: #666666">,</span> String<span style="color: #666666">.</span><span style="color: #7D9029">class</span><span style="color: #666666">);</span>
    CodeGenerator<span style="color: #666666">.</span><span style="color: #7D9029">entityDefinitionXml2Class</span><span style="color: #666666">(</span>domainName<span style="color: #666666">,</span> entityXmlFile<span style="color: #666666">,</span> srcPath<span style="color: #666666">,</span> entityMode<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">null</span><span style="color: #666666">,</span> propName2MethodName<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">false</span><span style="color: #666666">);</span>
    <span style="color: #408080; font-style: italic">// Or generate the entity class with fluent setter method.</span>
    <span style="color: #408080; font-style: italic">// CodeGenerator.entityDefinitionXml2Class(domainName, entityXmlFile, srcPath, entityMode, null, propName2MethodName, true);</span>
<span style="color: #666666">}</span>

<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">static</span> String <span style="color: #0000FF">propName2MethodName</span><span style="color: #666666">(</span>String propName<span style="color: #666666">)</span> <span style="color: #666666">{</span>
    Set<span style="color: #666666">&lt;</span>String<span style="color: #666666">&gt;</span> properNames <span style="color: #666666">=</span> N<span style="color: #666666">.</span><span style="color: #7D9029">asSet</span><span style="color: #666666">(</span><span style="color: #BA2121">&quot;gui&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;udid&quot;</span><span style="color: #666666">);</span>

    <span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">(</span>properNames<span style="color: #666666">.</span><span style="color: #7D9029">contains</span><span style="color: #666666">(</span>propName<span style="color: #666666">))</span> <span style="color: #666666">{</span>
        <span style="color: #008000; font-weight: bold">return</span> propName<span style="color: #666666">.</span><span style="color: #7D9029">toUpperCase</span><span style="color: #666666">();</span>
    <span style="color: #666666">}</span> <span style="color: #008000; font-weight: bold">else</span> <span style="color: #666666">{</span>
        <span style="color: #008000; font-weight: bold">return</span> N<span style="color: #666666">.</span><span style="color: #7D9029">capitalize</span><span style="color: #666666">(</span>propName<span style="color: #666666">);</span>
    <span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</pre></div>

			<p></p>
			<p>Here is the sample code for the generated entity class:  <a
				href="./Account_1.java">Account.java</a>, or with fluent setter method: <a
				href="./Account_2.java">Account.java</a>. It's more convenient to set multiple property values by fluent setter method, for example:</p>
<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #408080; font-style: italic">// Without fluent setter method</span>
Account account <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Account<span style="color: #666666">();</span>
account<span style="color: #666666">.</span><span style="color: #7D9029">setFirstName</span><span style="color: #666666">(</span><span style="color: #BA2121">&quot;jack&quot;</span><span style="color: #666666">);</span>
account<span style="color: #666666">.</span><span style="color: #7D9029">setLastName</span><span style="color: #666666">(</span><span style="color: #BA2121">&quot;ye&quot;</span><span style="color: #666666">);</span>

<span style="color: #408080; font-style: italic">// With fluent setter method.</span>
Account account <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Account<span style="color: #666666">().</span><span style="color: #7D9029">setFirstName</span><span style="color: #666666">(</span><span style="color: #BA2121">&quot;jack&quot;</span><span style="color: #666666">).</span><span style="color: #7D9029">setLastName</span><span style="color: #666666">(</span><span style="color: #BA2121">&quot;ye&quot;</span><span style="color: #666666">);</span>
</pre></div>



			<p></p>
			<li><b>Step 5: Generate the classes for property/column names by the xml file if necessary.</b> The String constants in the classes will help you to avoid writing magic Strings in your code and could make your application faster as well.</li>
<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">static</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">entityDefinitionXml2PropNameTable</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
    String srcPath <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;./src/&quot;</span><span style="color: #666666">;</span>
    String domainName <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;codes&quot;</span><span style="color: #666666">;</span>
    File entityXmlFile <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> File<span style="color: #666666">(</span><span style="color: #BA2121">&quot;./resources/codesEntityDefinition.xml&quot;</span><span style="color: #666666">);</span>
    CodeGenerator<span style="color: #666666">.</span><span style="color: #7D9029">entityDefinitionXml2PropNameTable</span><span style="color: #666666">(</span>domainName<span style="color: #666666">,</span> entityXmlFile<span style="color: #666666">,</span> srcPath<span style="color: #666666">);</span>
<span style="color: #666666">}</span>

<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">static</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">entityDefinitionXml2ColumnNameTable</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
    String srcPath <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;./src/&quot;</span><span style="color: #666666">;</span>
    String domainName <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;codes&quot;</span><span style="color: #666666">;</span>
    File entityXmlFile <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> File<span style="color: #666666">(</span><span style="color: #BA2121">&quot;./resources/codesEntityDefinition.xml&quot;</span><span style="color: #666666">);</span>
    CodeGenerator<span style="color: #666666">.</span><span style="color: #7D9029">entityDefinitionXml2ColumnNameTable</span><span style="color: #666666">(</span>domainName<span style="color: #666666">,</span> entityXmlFile<span style="color: #666666">,</span> srcPath<span style="color: #666666">);</span>
<span style="color: #666666">}</span>
</pre></div>
<p></p>
<p>Here are the samples for generated codes: <a href="./CodesPNL.java">CodesPNL.java</a>, <a href="./CodesCNL.java">CodesCNL.java</a>.
(Naming convention: The class name for property names is: domainName + "PNL". Here the name is: "CodesPNL"; The class name for column names is: domainName + "CNL". Here the name is: "CodesCNL")</p>

			<p></p>
			<li><b>Step 6: Generate the class for SQL mapper ids by the SQL mapper xml file if necessary:</b></li>
<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">static</span> <span style="color: #B00040">void</span> <span style="color: #0000FF">generateSQLMapperIdTable</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
    String srcPath <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;./src/&quot;</span><span style="color: #666666">;</span>
    <span style="color: #008000; font-weight: bold">final</span> String sqlMapperXmlFile <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;./resources/codesSQLMapper.xml&quot;</span><span style="color: #666666">;</span>
    <span style="color: #008000; font-weight: bold">final</span> String targetClassName <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;CodesSQLMapperIdList&quot;</span><span style="color: #666666">;</span>

    CodeGenerator<span style="color: #666666">.</span><span style="color: #7D9029">generateSQLMapperIdTable</span><span style="color: #666666">(</span>sqlMapperXmlFile<span style="color: #666666">,</span> srcPath<span style="color: #666666">,</span> packageName<span style="color: #666666">,</span> targetClassName<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">null</span><span style="color: #666666">);</span>
<span style="color: #666666">}</span>
</pre></div>

<p></p>
<p>Here are the generated class: <a href="./CodesSQLMapperIdList.java">CodesSQLMapperIdList.java</a> and the sql mapper xml file: <a href="./codesSQLMapper.xml">codesSQLMapper.xml</a>, and the schema: <a href="./SQLMapper.xsd">SQLMapper.xsd</a>. sql mapper(pre-defined SQL in xml file) is supported by SQLExecutor, CassandraExecutor, CouchbaseExecutor</p>



		<p></p>
		<p>It will accelerate the development and make your codes much cleaner by the above auto-generated classes. It's a lot of efforts we have spent to write this utility class to generate the pretty-formatted codes. We hope you will like it and enjoy programming with it. Refer to project <a href="./Helloabacus-common.zip">Helloabacus-common.zip</a> for the complete codes.</p>

		</div>

	</div>
</body>

</html>